/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

qx.Class.define("bip.ui.form.survey.AnswerFieldSlider",
{
  extend : qx.ui.core.Widget,
  
  implement : [
               qx.ui.form.INumberForm,
               qx.ui.form.IForm
             ],
  include : [
     qx.ui.form.MForm
  ],

  construct : function()
  {
    this.base(arguments);
    
    // initialize the layout
    var layout = new qx.ui.layout.Grid();
    this._setLayout(layout);
    
    // create the widgets
    this._createChildControl("slider-box");
  },

  properties :
  {
  	appearance :
    {
      refine : true,
      init : "answer-field-slider"
    },
    
    minvalue :
    {
      check : "Integer",
      apply : "_applyMinvalue",
      init : 0
    },
    
    maxvalue : {
    	check: "Integer",
    	 apply : "_applyMaxvalue",
       init : 10
    },
    
    value :
    {
      check : "Integer",
      nullable : true,
      event : "changeValue",
      apply : "_applyValue"
    }
   
  },

  members :
  { 
    // overridden
    _createChildControlImpl : function(id)
    {
      var control;

      switch(id)
      {        
      	
        case "slider":
        	control = new qx.ui.form.Slider();
          break;
          
        case "slider-box":
        	// Add minValue, maxValue and current value upside the slider 
        	var grid = new qx.ui.layout.Grid();
          var container = new qx.ui.container.Composite(grid);
//          container.setPadding(20);
          container.setWidth(200);
          container.setMaxWidth(200);
//          container.setHeight(40);
          grid.setSpacing(2);
          grid.setColumnFlex(0, 1);
          grid.setColumnFlex(1, 1);
          grid.setColumnFlex(2, 1);
          grid.setColumnAlign(0, "left", "bottom");
          grid.setColumnAlign(1, "center", "bottom");
          grid.setColumnAlign(2, "right", "bottom");
          // Create the slider and the labels
          var slider = this.getChildControl("slider")
          slider.setHeight(15);
          slider.setKnobFactor(0.1);
          slider.setPageStep(1);
          var minimumLabel = new qx.ui.basic.Label();
          var maximumLabel = new qx.ui.basic.Label();
          var valueLabel = new qx.ui.basic.Label();
          // Now build the widget
          slider.setOrientation("horizontal");
          valueLabel.setWidth(100);
          valueLabel.setTextAlign("center");
          container.add(minimumLabel, {row: 0, column: 0});
          container.add(valueLabel, {row: 0, column: 1});
          container.add(maximumLabel, {row: 0, column: 2});
          container.add(slider, {row: 1, column: 0, colSpan: 3, rowSpan: 1});
          grid.setRowHeight(2, 20);
          // Binding
          slider.bind("value",valueLabel,"value");
          slider.bind("value",this,"value");
          slider.bind("minimum",minimumLabel,"value");
//          slider.addListener("changeValue",function(evt) {
//          	valueLabel.setValue("Value:"+evt.getData());
//          	this.getChildControl("slider-prefix").setValue("Answer:"+evt.getData());
//          },this);
          slider.bind("maximum",maximumLabel,"value");
          slider.bind("value",this,"value");
          control = container ;
          
          this._add(control,{row:1,column:1});
          break;
      }

      return control || this.base(arguments, id);
    },
    
    // property apply
    _applyMinvalue : function(value, old)
    {
    	if (this.getChildControl("slider").getMaximum() < value) {
    		this.getChildControl("slider").setMaximum(value);
    	}
    	if (this.getChildControl("slider").getValue() < value) {
    		this.getChildControl("slider").setValue(value);
    	}
      this.getChildControl("slider").setMinimum(value);
    },
    
    // property apply
    _applyMaxvalue : function(value, old)
    {
    	if (this.getChildControl("slider").getMinimum() > value) {
    		this.getChildControl("slider").setMinimum(value);
    	}
    	if (this.getChildControl("slider").getValue() > value) {
    		this.getChildControl("slider").setValue(value);
    	}
      this.getChildControl("slider").setMaximum(value);
    },
    
    _applyValue: function(value,old) {
    	this.getChildControl("slider").slideTo(value);
    }
    
    
  },
  
  destruct : function()
  {
    
  }
});